<?php

require "../types.php";
require "../utility.php";

$conn = connect_mysql();

$user_id = getUserIdBySessionId($conn);

http_response_code(403);

if ($user_id !== null) {
    http_response_code(200);
    $query_str = "SELECT bookID, cnt FROM Cart WHERE userID = '$user_id';";
    $query = $conn->query($query_str);

    $cart = [];
    while ($cart_item_data = $query->fetch_row()) {
        $cart_item = CartItem::fromArray($cart_item_data);
        array_push($cart, $cart_item);
    }

    $query->close();

    echo json_encode($cart);
}

$conn->close();

?>